#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableView>
#include <QStandardItemModel>
#include <QItemSelection>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tableView = new QTableView;
    setCentralWidget(tableView);

    QStandardItemModel *model = new QStandardItemModel(8, 4);
    tableView->setModel(model);


    QItemSelectionModel *selectionModel = tableView->selectionModel();
    QModelIndex topLeft = model->index(0, 0, QModelIndex());
    QModelIndex bottomRight = model->index(model->rowCount()-1,
                                           model->columnCount()-1, QModelIndex());
    QItemSelection selection(topLeft, bottomRight);
    connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(updateSelections(QItemSelection,QItemSelection)));
    selectionModel->select(selection, QItemSelectionModel::Select);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateSelections(const QItemSelection &selection, const QItemSelection &deselection)
{
    QModelIndexList list = selection.indexes();
    QModelIndex index;

    foreach (index, list) {
        QString text = QString("(%1, %2)").arg(index.row()).arg(index.column());
        tableView->model()->setData(index, text);
    }

    list = deselection.indexes();
    foreach (index, list) {
        tableView->model()->setData(index, "");
    }
}
